import Vue from 'vue'
import Router from 'vue-router'

import AdminPage from '../page/admin.vue'
import HomePage from '../page/home.vue'
import LoginPage from '../page/login/login.vue'

import TaskListPage from '../page/task/task-list.vue'
import UserListPage from '../page/user/user-list.vue'

import MoneyAddPage from '../page/money/add.vue'
import MoneyDelPage from '../page/money/del.vue'
import MoneyCountPage from '../page/money/count.vue'
import UnpayPage from '../page/money/unpay.vue'




Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '*',
      redirect: '/admin/home'
    },
    {
      path: '/admin',
      name: 'AdminPage',
      redirect: '/admin/home',
      component: AdminPage,
      children: [{
        path: 'login',
        name: 'LoginPage',
        meta: {
          auth: false,
        },
        component: LoginPage
      },
      {
        path: 'home',
        name: 'HomePage',
        redirect: '/admin/home/task-list',
        meta: {
          auth: true,
        },
        component: HomePage,
        children: [
          {
            path: 'task-list',
            name: 'TaskListPage',
            component: TaskListPage
          },
          {
            path: 'user-list',
            name: 'UserListPage',
            component: UserListPage
          },
          {
            path: 'money-add',
            name: 'MoneyAddPage',
            component: MoneyAddPage
          },
          {
            path: 'money-del',
            name: 'MoneyDelPage',
            component: MoneyDelPage
          },
          {
            path: 'money-count',
            name: 'MoneyCountPage',
            component: MoneyCountPage
          },
          {
            path: 'money-unpay',
            name: 'UnpayPage',
            component: UnpayPage
          }
        ]
      },]
    },
    // {
    //   path: '/login',
    //   name: 'LoginPage',
    //   meta: {
    //     auth: false,
    //   },
    //   component: LoginPage
    // },
    // {
    //   path: '/home',
    //   name: 'HomePage',
    //   redirect: '/home/task-list',
    //   meta: {
    //     auth: true,
    //   },
    //   component: HomePage,
    //   children: [
    //     {
    //       path: 'task-list',
    //       name: 'TaskListPage',
    //       component: TaskListPage
    //     },
    //     {
    //       path: 'user-list',
    //       name: 'UserListPage',
    //       component: UserListPage
    //     },
    //     {
    //       path: 'money-add',
    //       name: 'MoneyAddPage',
    //       component: MoneyAddPage
    //     },
    //     {
    //       path: 'money-del',
    //       name: 'MoneyDelPage',
    //       component: MoneyDelPage
    //     },
    //     {
    //       path: 'money-count',
    //       name: 'MoneyCountPage',
    //       component: MoneyCountPage
    //     }
    //   ]
    // },
  ]
})

/**
 * 一个路由钩子，可以验证是否登陆
 */
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (localStorage.getItem('username') !== null) {
      next();
    } else {
      alert('please login')
      next({
        path: '/'
      })
    }
  } else {
    next();
  }
});

export default router
